package cn.itcast.nems.journal.util;

import cn.itcast.nems.approval.service.ApprovalFormService;
import cn.itcast.nems.base.dto.ClazzDTO;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.constant.PaymentTypeEum;
import cn.itcast.nems.common.util.NumberUtil;
import cn.itcast.nems.oldems.dto.CirculationWay;
import cn.itcast.nems.oldems.dto.StudentJoinClassInfo;
import cn.itcast.nems.oldems.dto.StudentRecordCirculation;
import cn.itcast.nems.studentrecord.constant.OrderSourceEnum;
import cn.itcast.nems.studentrecord.dao.condition.ApprovalType;
import cn.itcast.nems.studentrecord.dao.dataobject.JournalRepeatDO;
import cn.itcast.nems.studentrecord.dao.entity.JournalRepeat;
import cn.itcast.nems.studentrecord.dto.StudentRecordDTO;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
/**
 * <p>
 * 重读凭单工具类
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/14 10:35 AM
 **/
public class JournalRepeatUtil {
    private final static Logger log = LoggerFactory.getLogger(JournalRepeatUtil.class) ;
    private JournalRepeatUtil() {}

    /**
     * <p>
     * 根据审批类型获取重读凭单分页查询条件
     * </p>
     *
     * @param journalIds                                凭单ID
     * @param repeatDO                                  重读凭单
     * @param approvalType                              审批类型
     * @author zhouwei
     * @since 2023/12/8 1:34 PM
     **/
    public static void invokeJournalRepeatDOByApprovalType(
                                                            List<String> journalIds,
                                                            JournalRepeatDO repeatDO,
                                                            ApprovalType approvalType)
    {
        repeatDO.setApprovalType(approvalType);
        repeatDO.setJournalType(ApprovalFormService.JournalType.REPEAT.name());
        if (approvalType == ApprovalType.MY_COMMIT) {
            repeatDO.setFlowCreatedBy(AccountHolder.getRequiredAccount());
            repeatDO.setCreatedBy(AccountHolder.getRequiredAccount());
        } else if (approvalType == ApprovalType.WAIT_APPROVAL) {
            // 待办
            repeatDO.setFormIds(journalIds);
            repeatDO.setNotDraft(true);
        } else if (approvalType == ApprovalType.APPROVED) {
            // 已办
            repeatDO.setFormIds(journalIds);
            repeatDO.setFlowCreatedBy(AccountHolder.getRequiredAccount());
            repeatDO.setNotDraft(true);
        } else if (approvalType == ApprovalType.ALL) {
            // 全部
            repeatDO.setNotDraft(true);
        }
    }

    /**
     * <p>
     * 获取重读凭单工作流参数
     * process_key: process_82092143ef8b4053bb4451909b9bdb3b
     * 参数变量：
     * 1、是否同一校区  is_same_campus （"true"， "false"）
     * 2、目标班级校区名称  	target_clazz_school_name（"BEIJING"）
     * 3、是否特殊审批 special_approval （"true"， "false"）
     * </p>
     *
     * @param specialFlag                               是否特殊审批
     * @param schoolId                                  校区ID
     * @author zhouwei
     * @since 2023/12/8 1:33 PM
     **/
    public static HashMap<String, Object> getVariables(Boolean specialFlag,String schoolId) {
        HashMap<String, Object> map = new HashMap<>();
        // 1、办理事项  handle_matters  （ "线下转线上博学谷" ， "转班"）
        map.put("handle_matters", OrderSourceEnum.REPEAT.getCname());
        // 2、是否特殊审批 special_approval （"true"， "false"）
        map.put("special_approval", Objects.isNull(specialFlag) || !specialFlag ? "false": "true");
        map.put("target_clazz_school_name",schoolId) ;
        map.put("is_same_campus",true) ;
        // 目标班级对应校区 code
        map.put("transfer_school",schoolId) ;
        log.info("【重读凭单-获取工作流参数】参数：{}" , JSON.toJSONString(map));
        return map ;
    }


    /**
     * <p>
     * 将升级凭单学籍信息同步至老EMS
     * </p>
     *
     * @param repeat 凭单数据
     * @author zhouwei
     * @since 2024/3/19 9:19 AM
     **/
    public static StudentRecordCirculation wrapCirculation(JournalRepeat repeat , StudentRecordDTO originStudentRecord , ClazzDTO clazzDTO , Integer studentId ) {
        // AccountInfo accountInfo = new AccountInfo(employee.getEmail(), employee.getName(), employee.getJobNumber(), null);
        StudentJoinClassInfo origin = StudentJoinClassInfo
                .builder()
                .clazzId(originStudentRecord.getClazzId())
                .inDate(originStudentRecord.getInDate())
                .outDate(repeat.getOriginClazzOutDate())
                .paymentType(PaymentTypeEum.FIRSTPAYMENT)
                .price(NumberUtil.convertToCent(originStudentRecord.getPrice()))
                .serviceDays(0)
                .actualServiceDays(0)
                .build();
        StudentJoinClassInfo target = StudentJoinClassInfo
                .builder()
                .clazzId(repeat.getTargetClazzId())
                .inDate(repeat.getTargetClazzInDate())
                .outDate(null)
                .paymentType(PaymentTypeEum.FIRSTPAYMENT)
                .price(repeat.getAmount())
                .openClass(Objects.nonNull(clazzDTO.getStartDate()) &&  !repeat.getTargetClazzInDate().isBefore(clazzDTO.getStartDate()))
                .build();
        return  StudentRecordCirculation
                .builder()
                .studentId(studentId)
                .originStudentJoinClassInfo(origin)
                .targetStudentJoinClassInfo(target)
                .circulationWay(CirculationWay.DOWNGRADE)
                .operator(AccountHolder.getRequiredAccountInfo())
                // .operator(new AccountInfo("lvning@itcast.cn", "吕宁", "93653", null))
                .build();
    }

}
